package com.xyyhome.web.love.controller;

import com.xyyhome.common.YAjaxException;
import com.xyyhome.common.YConstant;
import com.xyyhome.web.love.domain.YLDynamic;
import com.xyyhome.web.love.domain.YLDynamicComment;
import com.xyyhome.web.love.domain.YLDynamicCommentRepository;
import com.xyyhome.web.love.domain.YLDynamicRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 动态
 * Created by ty850 on 2017/5/21.
 */
@Controller
@RequestMapping("/love/dynamic")
public class YLDynamicController extends YLBaseController {

    @Autowired
    private YLDynamicRepository ylDynamicRepository;

    @Autowired
    private YLDynamicCommentRepository yLDynamicCommentRepository;



    @RequestMapping
    public String index() {
        return "";
    }

    /**
     * 评论动态
     * @param id 动态主键
     * @param content 评论的内内容
     * @return ajax数据
     */
    @RequestMapping("/comment")
    @ResponseBody
    public Object comment(String id, String content) {
        if (StringUtils.isEmpty(content))
            throw new YAjaxException("评论的内容不能为空");
        YLDynamic dynamic = ylDynamicRepository.getOne(id);
        if (dynamic == null)
            throw new YAjaxException("评论的动态不存在");

        // 设置评论内容并保存
        YLDynamicComment comment = new YLDynamicComment();
        comment.setYldcContent(content);// 内容
        comment.setYldcCreateTime(new Date());// 日期
        comment.setYldcDynamicId(id);// 评论的动态id
        comment.setYldcState(YConstant.STATE_NORMAL);// 评论状态
        comment.setYldcUserId(null);// TODO 当前用户id
        yLDynamicCommentRepository.save(comment);


        return success("评论成功", comment);
    }


}
